﻿using Admin.NET.Core;
using Furion.DatabaseAccessor;
using Furion.DatabaseAccessor.Extensions;
using Furion.DependencyInjection;
using Furion.DynamicApiController;
using Furion.FriendlyException;
using Furion.RemoteRequest;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Admin.NET.Application
{
    /// <summary>
    /// 巡检评分管理
    /// </summary>
    [Route("api/scoremanager")]
    [ApiDescriptionSettings("道路巡检", Name = "ScoreManager", Order = 100)]
    public class ScoreService : IDynamicApiController,IScoreService, IScoped
    {
        /// <summary>
        /// 获取评分信息
        /// </summary>
        /// <returns></returns>
        public async Task<object> GetScoreInfo()
        {
            var en = Db.GetRepository<Plan_Score>().FirstOrDefault().Adapt<ScoreInfoDto>();
            if (en == null)
                en = new ScoreInfoDto();
            return en;
        }
        /// <summary>
        /// 评分更新
        /// </summary>
        /// <param name="score">更新信息</param>
        /// <returns></returns>
        public async Task<object> PostScoreInfo(ScoreInfoDto score)
        {
            var rp = Db.GetRepository<Plan_Score>();
            var temp = score.Adapt<Plan_Score>();
            var en = rp.FirstOrDefault(x => x.Id == score.Id);
            if (en == null)
            {
                throw Oops.Oh("数据不存在！");
            }
            else
            {
                en.Ponding = temp.Ponding;
                en.WhiteGarbage = temp.WhiteGarbage;
                en.WaterGrateBlockage = temp.WaterGrateBlockage;
                en.SideDitchSediment = temp.SideDitchSediment;
                en.SideDitchFallenLeaves = temp.SideDitchFallenLeaves;
                en.UpdateExcludeNow(new[] { nameof(Plan_Score.Id) });
                return "更新成功";
            }
        }
    }
}
